package com.youxue.service;

import com.youxue.dao.UserMapper;
import com.youxue.entity.Pager;
import com.youxue.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by 15c on 2018/1/15.
 */
@Service("User")
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public User select(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User selectUserIdByEmail(User email) {
        return userMapper.selectUserIdByEmail(email);
    }

    @Override
    public User showUserByEmail(String email) {
        return userMapper.selectUserByEmail(email);
    }

    @Override
    public User selectUserIdByPhone(User phone) {
        return userMapper.selectUserIdByPhone(phone);
    }

    @Override
    public int addUser(User user) {
        return userMapper.insertAUser(user);
    }

    @Override
    public User showUserPartInfo(Integer userId) {
        return userMapper.selectAUserPartInfo(userId);
    }

    @Override
    public int resetPasswordByemail(User user) {
        return userMapper.updatePasswordByemail(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public double showAUserBalance(Integer userId) {
        return userMapper.selectAUserBalance(userId);
    }

    @Override
    public int reviseAUserBalance(User user) {
        return userMapper.updateAUserBalance(user);
    }

    @Override
    public int reviseBalanceByUserId(Integer userId,Double money) {
        return userMapper.addAUserBalanceByUserId(userId,money);
    }

    @Override
    public Pager<User> find(Integer pageOffset) {
        //一页多少条数据
        int pageSize = 6;
        Pager<User> pager = new Pager<User>();
        pager.setPageSize(pageSize);
        //一共有多少条数据
        int totalRecord = userMapper.selectUserTotal();
        pager.setTotalRecord(totalRecord);
        pager.setDatas(userMapper.selectUserByPage(pageOffset, pageSize));
        //判断多少共有多少页
        int resultPage = totalRecord / pageSize;
        //判断是否存在余数；若存在将多一页
        int resultRemainder = totalRecord % pageSize;
        if (0 < resultRemainder) {
            resultPage = resultPage + 1;
            pager.setTotalPage(resultPage);
        } else {
            pager.setTotalPage(resultPage);
        }

        return pager;
    }

    @Override
    public Pager<User> findByCommonAdmin(Integer pageOffset) {
        //一页多少条数据
        int pageSize = 6;
        Pager<User> pager = new Pager<User>();
        pager.setPageSize(pageSize);
        //一共有多少条数据
        int totalRecord = userMapper.selectUserTotalByCommonAdmin();
        pager.setTotalRecord(totalRecord);
        pager.setDatas(userMapper.selectUserByPageByCommonAdmin(pageOffset, pageSize));
        //判断多少共有多少页
        int resultPage = totalRecord / pageSize;
        //判断是否存在余数；若存在将多一页
        int resultRemainder = totalRecord % pageSize;
        if (0 < resultRemainder) {
            resultPage = resultPage + 1;
            pager.setTotalPage(resultPage);
        } else {
            pager.setTotalPage(resultPage);
        }

        return pager;
    }

    @Override
    public User findUserByRetrieval(String keyword) {
        return userMapper.selectUserByRetrieval(keyword);
    }

    @Override
    public User findAUserInfo(Integer userId) {
        return userMapper.selectAUserInfo(userId);
    }
}
